/**
 * Created by Administrator on 2017-05-26.
 */
String.prototype.temp = function(obj) {
    return this.replace(/\$\w+\$/gi, function(matchs) {
        var returns = obj[matchs.replace(/\$/g, "")];
        return (returns + "") == "undefined"? "": returns;
    });
};

function resultcheck(result, redurl) {

    if (result) {
        var code = result.code;
        var msg = result.msg;
        var res = result.data;
        if (code == 0) {
            if (redurl) {
                window.location.href = redurl;
            }
        } else {
            layer.msg(msg);
        }
    } else {
        layer.msg("返回为空");
    }
}

//消息类 ajax 请求方法
function ajax_post(url, data, redurl) {
    $.ajax({
        type: "POST",
        url: url,
        data: data,// 你的formid
        dataType: 'JSON',
        error: function (result) {
            layer.msg("请求错误！");
        },
        success: function (data) {
            resultcheck(data, redurl);
        }
    });
}


// 异常处理
function errorHandle(response , error){
    if( typeof error =="function"){
        error();
    }else{
        layer.msg("请求错误!");
    }
}

// 成功返回数据调用
function successHandle(response , fuc){
    if (response.status == 200) {
        var res = response.data;

        if(res){
            var code = res.code;
            var msg = res.msg;
            var data = res.data;
            if (code == 0) {

                // 请求成功
                if( typeof fuc =="function"){
                    fuc(data);
                }

            } else if (code == -1) {
                layer.msg(msg);

                // 未登录
            } else if (code == -2) {
                window.location.href="/index.php/Home/index/login"
            }
        }else{
            layer.msg("没有数据!");
        }
    } else {
        layer.msg("请求错误:" + response.status);
    }
}

// 获取angular中app
function getApp(appStr, para) {
    var app = angular.module(appStr, para);
    app.config(['$httpProvider', function ($httpProvider){
        $httpProvider.defaults.headers.method = 'POST';
        $httpProvider.defaults.headers.common = {"Content-Type" : 'application/x-www-form-urlencoded'};
        $httpProvider.defaults.headers.post = {"Content-Type" : 'application/x-www-form-urlencoded'};
        $httpProvider.defaults.headers.get = {"Content-Type" : 'application/x-www-form-urlencoded'};
        $httpProvider.defaults.transformRequest = function (obj) {
            var str = [];
            for (var p in obj) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
            return str.join("&");
        };
    }]);
    return app;
}

//获取url中的参数值
var getUrlParam = function(name){
    var search = document.location.search;
    var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
    var matcher = pattern.exec(search);
    var items = null;
    if(null != matcher){
        try{
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        }catch(e){
            try{
                items = decodeURIComponent(matcher[1]);
            }catch(e){
                items = matcher[1];
            }
        }
    }
    return items;
};

//Agularjs http 请求方法
function http_post($http ,url, data, redurl) {
    $http({
        method: 'POST',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformRequest: function (obj) {
            var str = [];
            for (var p in obj) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
            return str.join("&");
        },
        url: url
    }).then(function successCallback(response) {
        if (response.data.msg != null || response.data.msg != '') {
            layer.msg(response.data.msg, {time: 1000});
        } else if (response.data.data.msg != null || response.data.data.msg != '') {
            layer.msg(response.data.data.msg, {time: 1000});
        }
        if (redurl != null) {
            setTimeout(function () {
                successHandle(response, function () {
                    window.location.href = redurl
                });
            }, 1000)
        }
    }, function errorCallback(response) {
        errorHandle(response, error);
    });
}

// Agularjs http 请求方法
function httpPost($http ,url, data, success ,error) {
    $http({
        method: 'POST',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformRequest: function (obj) {
            var str = [];
            for (var p in obj) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
            return str.join("&");
        },
        url: url
    }).then(function successCallback(response) {
        if(response.data.code == -1){
            layer.msg(response.data.msg,{time:1000});
        }
        successHandle(response, success);
    }, function errorCallback(response) {
        errorHandle(response,error);
    });
}

//angularjs下拉加载
function scrolldownload($scope,$http,url) {
    // 当前页数
    $scope.currentPage = 0;
    // 加载完毕标志
    $scope.endflg =  0;
    // 防止重复加载
    $scope.busy = false;
    // 存放数据
    $scope.message = [];
    // 请求数据方法
    $scope.loadMore = function(){
        if($scope.endflg == 0){
            if($scope.busy){
                return false;
            }
            $scope.busy = true;
            $http.get(url+'?page='+$scope.currentPage)
                .success(function(data){
                    $scope.busy = false;
                    if(data.data != '' && data.data != null){
                        if($scope.currentPage > 0){
                            layer.msg("正在加载...", {time:500});
                        }
                    } else {
                        layer.msg("已加载完毕", {time:500});
                        $scope.endflg = 1;
                        return false;
                    }
                    $scope.currentPage++;
                    // 组织数据
                    for(var i in data.data){
                        $scope.message.push(data.data[i]);
                    }
                });
        }else{
            layer.msg("已加载完毕", {time:500});
        }
    };
    // 进页面先加载一次数据
    $scope.loadMore();

    // 页面可视区域高度
    var winH = $(window).height();
    var scrollHandler = function () {
        // 页面高度
        var pageH = $(document.body).height();
        // 滚动条top
        var scrollT = $(window).scrollTop();
        var toend = (pageH - winH - scrollT) / winH;
        if (toend == 0) {
            $scope.loadMore();
        }
    }
    // 定义鼠标滚动事件
    $(window).scroll(scrollHandler);
}

//angularjs下拉加载post请求
function scrolldownloadPost($scope,$http,url) {
    // 当前页数
    $scope.currentPage = 0;
    // 加载完毕标志
    $scope.endflg =  0;
    // 防止重复加载
    $scope.busy = false;
    // 存放数据
    $scope.message = [];
    // 请求数据方法
    $scope.loadMore = function(){
        if($scope.endflg == 0){
            if($scope.busy){
                return false;
            }
            $scope.busy = true;
            $http({
                method: 'POST',
                data: $scope,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                },
                url: url
            }).then(function successCallback(response) {
                $scope.busy = false;
                if(response.data.data != '' && response.data.data != null){
                    if($scope.currentPage > 0) {
                        layer.msg("正在加载...", {time: 500});
                    }
                } else {
                    layer.msg("已加载完毕", {time:500});
                    $scope.endflg = 1;
                    return false;
                }
                $scope.currentPage++;
                if(response.data.code == -1){
                    layer.msg(response.data.msg,{time:1500})
                }else{
                    // 组织数据
                    for(var i in response.data.data){
                        $scope.message.push(response.data.data[i]);
                    }
                }
            }, function errorCallback(response) {
                errorHandle(response);
            });
        }else{
            layer.msg("没有更多了...", {time:500});
        }
    };
    // 进页面先加载一次数据
    $scope.loadMore();

    // 页面可视区域高度
    var winH = $(window).height();
    var scrollHandler = function () {
        // 页面高度
        var pageH = $(document.body).height();
        // 滚动条top
        var scrollT = $(window).scrollTop();
        var toend = (pageH - winH - scrollT) / winH;
        if (toend == 0) {
            $scope.loadMore();
        }
    }
    // 定义鼠标滚动事件
    $(window).scroll(scrollHandler);
}